//
//  BIZMovieSitcomListView.m
//  biz
//
//  Created by zhangfuwei on 2017/3/23.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZMovieSitcomListView.h"
#import "BIZLivePlayBackCell.h"
#import "BIZSitcomIntroduceTableViewCell.h"
#import "BIZSitcomTeacherInfoCell.h"
#import "BIZMoviePlaySitcomListDataModel.h"
#import "BIZNoConsultorCell.h"

static CGFloat  kVideoWatchNumLabH = 29;
static NSString *kSitcomListTabCellResueID = @"kSitcomListTabCellResueID";
static NSString *kNodataCellResueID = @"kNodataCellResueID";
static NSString *kDefaultPlayingVideoID = @"-123456789";

typedef enum : NSUInteger {
    BIZMovieSitcomListViewDisplayChapter = 0,
    BIZMovieSitcomListViewDisplayDetail,
    BIZMovieSitcomListViewDisplayTeacherInfo
} BIZMovieSitcomListViewDisPlayState;

@interface BIZMovieSitcomListView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *sortedKeys;

@end

@implementation BIZMovieSitcomListView{
    UIButton     *_videoWatchNumBtn; // 观看次数
    UITableView *_tableView;    // 内容视图
    
    NSString    *_isPlayingVideoID;
    BIZMovieSitcomListViewDisPlayState _displayState;
    int         _lastId;/* 数据源最后的id*/
}

-(instancetype)init {
    self = [super init];
    if (self) {
        [self setConfig];
        [self loadUI];
        self.backgroundColor = [UIColor blackColor];
    };
    return self;
}

- (void)setConfig {
    _isPlayingVideoID = kDefaultPlayingVideoID;
    _lastId = 1;
    self.dataSourceDic = [NSMutableDictionary dictionary];
}

- (void)loadUI {
    //  观看次数
    _videoWatchNumBtn = [[UIButton alloc] init];
    _videoWatchNumBtn.backgroundColor = [UIColor blackColor];
    _videoWatchNumBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _videoWatchNumBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    _videoWatchNumBtn.titleLabel.font = CHINESE_SYSTEM(14);
    
    // 内容视图
    _tableView  = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
//    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreSitcomList)];

    [_tableView registerClass:[BIZLivePlayBackCell class] forCellReuseIdentifier:kSitcomListTabCellResueID];
    [_tableView registerNib:[UINib nibWithNibName:@"BIZNoConsultorCell" bundle:nil] forCellReuseIdentifier:kNodataCellResueID];

    _tableView.delegate     = self;
    _tableView.dataSource   = self;
    _tableView.estimatedRowHeight   = 50;
    _tableView.rowHeight            = UITableViewAutomaticDimension;
    _tableView.tableFooterView      = [UIView new];
//    UIView  *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, kTableViewTopMargin)];
//    header.backgroundColor = [UIColor whiteColor];
//    _tableView.tableHeaderView = header;
    _tableView.separatorStyle       = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    
    [self addSubview:_videoWatchNumBtn];
    [self addSubview:_tableView];
}


- (void)layoutSubviews {
    [super layoutSubviews];
    _videoWatchNumBtn.frame = CGRectMake(0, 0, Main_Screen_Width, kVideoWatchNumLabH);
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_videoWatchNumBtn.mas_bottom);
        make.left.right.bottom.equalTo(self);
    }];
}


-(void)setDataSourceDic:(NSMutableDictionary *)dataSourceDic {
    _dataSourceDic = dataSourceDic;
    _sortedKeys = [NSMutableArray array];
    [dataSourceDic.allKeys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *firstKey = !_sortedKeys.count ? @"": _sortedKeys[0];
        NSString *lastKey  = _sortedKeys.lastObject;
        if (!idx) {
            [_sortedKeys addObject:key];
        }else if (key.intValue > firstKey.intValue){
            [_sortedKeys insertObject:key atIndex:0];
        }else if (key.intValue < lastKey.intValue) {
            [_sortedKeys addObject:key];
        }
    }];
    [_tableView reloadData];
}

#pragma mark -- delegate,datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSourceDic.count?:1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (!_sortedKeys.count) {
        return nil;
    }
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(18, 13,50,20)];
    label.text = _sortedKeys[section];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = HEXCOLOR(0x5ec1fa);
    label.font = CHINESE_SYSTEM(13);
    label.layer.cornerRadius = 10.f;
    label.layer.masksToBounds = YES;
    label.layer.borderColor = HEXCOLOR(0x5ec1fa).CGColor;
    label.layer.borderWidth = 1.0f;
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 35)];
    [header addSubview:label];
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (!_sortedKeys.count) {
        return 0;
    }
    return 35;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!_sortedKeys.count) {
        return 1;
    }

    NSString *key = _sortedKeys[section];
    NSArray *valueArr = [self.dataSourceDic valueForKey:key];
    DeLog(@"key---%@,sectionNum --%ld",key,valueArr.count);
    return valueArr.count?:1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (!_sortedKeys.count) { // 单集列表为空
        BIZNoConsultorCell *noDataCell = [tableView dequeueReusableCellWithIdentifier:kNodataCellResueID forIndexPath:indexPath];
        noDataCell.errorMsg = @"单集列表更新中";
        noDataCell.backgroundColor = [UIColor whiteColor];
        return noDataCell;
    }
    
    NSString *key = _sortedKeys[indexPath.section];
    NSArray *valueArr = [self.dataSourceDic valueForKey:key];
    BIZMoviePlaySitcomScrListDataModel *dataModel = valueArr[indexPath.row];
    
    BIZLivePlayBackCell *cell = [tableView dequeueReusableCellWithIdentifier:kSitcomListTabCellResueID forIndexPath:indexPath];
    
    cell.dataModel = dataModel;
    cell.isPlayingVideoID = _isPlayingVideoID;
    
    cell.isFirstCell = !indexPath.row;
    cell.isLastCell = indexPath.row == valueArr.count-1 ?YES:NO;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!_sortedKeys.count) {
        return;
    }

    NSString *key = _sortedKeys[indexPath.section];

    NSArray *valueArr = [self.dataSourceDic valueForKey:key];
    
    BIZMoviePlaySitcomScrListDataModel *dataModel = valueArr[indexPath.row];

    [_videoWatchNumBtn setTitle:[NSString stringWithFormat:@"正在播放:%@",dataModel.title] forState:UIControlStateNormal];
    
    !self.newBlock ?:self.newBlock(indexPath);
}

- (void)setTitle:(NSString *)title {
    _title = title;
    [_videoWatchNumBtn setTitle:[NSString stringWithFormat:@"正在播放:%@",title] forState:UIControlStateNormal];

}
- (void)reloadTableViewDataWithIsPlayingVideoID:(NSString *)videoID {
    _isPlayingVideoID = videoID;

    [_tableView reloadData];
}

- (void)stopRefresh
{
    !_tableView.mj_header.isRefreshing ? : _tableView.mj_header.endRefreshing;
    !_tableView.mj_footer.isRefreshing ? : _tableView.mj_footer.endRefreshing;
}

@end
